package com.talent.jiwen.teacher;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TeacherFragment extends BaseFragment {
    @BindView(R.id.gradeTv)
    TextView gradeTv;
    @BindView(R.id.arrowIv)
    ImageView arrowIv;
    @BindView(R.id.gradeLayout)
    RelativeLayout gradeLayout;
    @BindView(R.id.indicator)
    MagicIndicator indicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> subjects = new ArrayList<>();

    @Override
    protected void initData() {
        gradeTv.setText(getString(R.string.grade7));

        subjects.clear();
        subjects.add(getString(R.string.shouye));
        subjects.add(getString(R.string.shuxue));
        subjects.add(getString(R.string.yuwen));
        subjects.add(getString(R.string.yingyu));
        subjects.add(getString(R.string.wuli));
        subjects.add(getString(R.string.huaxue));
        subjects.add(getString(R.string.shengwu));

        fragments.clear();
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());
        fragments.add(new TeacherListFragment());


        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return subjects == null ? 0 : subjects.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(ContextCompat.getColor(getContext() , R.color.color_333333));
                colorTransitionPagerTitleView.setSelectedColor(ContextCompat.getColor(getContext() , R.color.common_app_color));
                colorTransitionPagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP , 16);
                colorTransitionPagerTitleView.setText(subjects.get(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                    }
                });

                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setColors(ContextCompat.getColor(getContext() , R.color.common_app_color));
                indicator.setLineHeight(4);
                return indicator;
            }
        });
        indicator.setNavigator(commonNavigator);

        viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return subjects.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return subjects.get(position);
            }
        });

        ViewPagerHelper.bind(indicator, viewPager);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_teacher;
    }

    @OnClick(R.id.gradeLayout)
    public void onViewClicked() {
    }
}
